package com.beiding.workordersystem.orders.common.service.impl;

import com.beiding.workordersystem.common.service.BaseService;
import com.beiding.workordersystem.common.standard.E;
import com.beiding.workordersystem.common.utils.QueryUtils;
import com.beiding.workordersystem.orders.common.dao.FlowUpDao;
import com.beiding.workordersystem.orders.generalorder.dao.OrderProcessDao;
import com.beiding.workordersystem.orders.common.entity.FlowUp;
import com.beiding.workordersystem.orders.generalorder.entity.OrderProcess;
import com.beiding.workordersystem.orders.common.service.FlowUpService;
import com.beiding.workordersystem.orders.looporder.dao.LoopProcessDao;
import com.beiding.workordersystem.orders.looporder.entity.LoopProcess;
import com.beiding.workordersystem.system.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FlowUpServiceImpl extends BaseService implements FlowUpService {

    @Autowired
    private FlowUpDao flowUpDao;

    @Autowired
    private FileService fileService;

    @Autowired
    private OrderProcessDao processDao;

    @Autowired
    private LoopProcessDao loopProcessDao;

    @Override
    public List<FlowUp> list(String id) {
        List<FlowUp> flowUps = flowUpDao.selectList(QueryUtils.getQuery(FlowUp.class).eq(FlowUp::getProcessId, id));
        if (flowUps.size() > 0) {
            for (FlowUp flowUp : flowUps) {
                List<String> list = fileService.listFileByHolderId(flowUp.getId());
                flowUp.setPictures(list);
            }
        }
        return flowUps;
    }

    @Override
    public void save(FlowUp flowUp) {
        E.isTrue(processDao.exist(QueryUtils.getQuery(OrderProcess.class).eq(OrderProcess::getId, flowUp.getProcessId())),"工序不存在");
        fillId(flowUp);
        if (flowUp.getPictures() != null) {
            fileService.claim(flowUp.getId(),flowUp.getPictures());
        }
        flowUp.setCreateTime(new Date());
        flowUpDao.insert(flowUp);

    }

    @Override
    public void saveLoop(FlowUp flowUp) {
        E.isTrue(loopProcessDao.exist(QueryUtils.getQuery(LoopProcess.class).eq(LoopProcess::getId, flowUp.getProcessId())),"工序不存在");
        fillId(flowUp);
        if (flowUp.getPictures() != null) {
            fileService.claim(flowUp.getId(),flowUp.getPictures());
        }
        flowUp.setCreateTime(new Date());
        flowUpDao.insert(flowUp);
    }

}
